﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class FND_ITM_PR : RibbonForm
    {
        public FND_ITM_PR()
        {
            InitializeComponent();
        }


        public string ConnectionString { get; set; }
        public string ItemCode { get; set; }
        public string Description { get; set; }
        public string UoM { get; set; }
        public double UnitPrice { get; set; }


        
        private void FND_ITM_PR_Load(object sender, EventArgs e)
        {
            cboItemGroup.SelectedIndex = 0;
            cboSearch.SelectedIndex = 0;
            
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            ItemManager oManager = new ItemManager();
            AddressBookManager oBPCard = new AddressBookManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable dt = new DataTable();

            if (cboSearch.SelectedIndex == 0)
            {
                dt = oManager.BrowsePurItmsByCode(cboItemGroup.Text, txtSearch.Text);
            }
            else
            {
                dt = oManager.BrowsePurItmsByName(cboItemGroup.Text, txtSearch.Text);
            }

            oManager.Close();

            gridControl1.DataSource = dt;
            gridControl1.Refresh();
        

        }

        private void btnChoose_Click(object sender, EventArgs e)
        {

            int iFocusRowHandle = gridView1.FocusedRowHandle;
            DevExpress.XtraGrid.Columns.GridColumnCollection oCols = gridView1.Columns;

            this.ItemCode = gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["ItemCode"]).ToString();
            this.Description = gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["Dscription"]).ToString();
            this.UoM = gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["UoM"]).ToString();
            this.UnitPrice = Convert.ToDouble(gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["UnitPrice"]));
            //if (lvi.SubItems[3].Text != "")
            //{
            //    this.UnitPrice = Convert.ToDouble(lvi.SubItems[3].Text);

            //}
            //else
            //{
            //    this.UnitPrice = 0;
            //}
            
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(sender, e);
            }
        }
    }
}
